package com.finddeer.bookkeeping.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.BillLab;
import com.finddeer.bookkeeping.BillPagerActivity;
import com.finddeer.bookkeeping.R;
import com.finddeer.bookkeeping.SearchActivity;
import com.finddeer.bookkeeping.adapter.BookkeepingContentFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2018/12/23 8:24 PM
 * function  : 实现记账页面的Fragment，托管TodayFragment以显示账单列表。包含添加、搜索、显示账单数目的菜单栏。
 */
public class BookkeepingFragment extends Fragment {
    //  绑定视图
    @BindView(R.id.bookkeeping_tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.bookkeeping_view_pager)
    ViewPager mViewPager;

    private boolean mSubtitleVisible;

    private static final String SAVED_SUBTITLE_VISIBLE = "subtitle";

    private BookkeepingContentFragmentAdapter mBookkeepingContentFragmentAdapter;
    private List<String> names; //  存储顶部导航栏的若干个标题

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);    //  声明此页面有右上角的菜单项（"添加""搜索"）
        initData(); //  初始化顶部导航names数据列表
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bookkeeping, container, false);  //  绑定布局
        ButterKnife.bind(this, view);

        //  设置FragmentManager()
        mBookkeepingContentFragmentAdapter = new BookkeepingContentFragmentAdapter(getChildFragmentManager());
        //  设置viewPager的适配器Adapter
        mViewPager.setAdapter(mBookkeepingContentFragmentAdapter);
        //  设置tabLayout与ViewPager联动
        mTabLayout.setupWithViewPager(mViewPager);

        // 更新适配器数据
        mBookkeepingContentFragmentAdapter.setList(names);

        if (savedInstanceState != null) {
            mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
        }
        return view;
    }

    private void initData() {
        names = new ArrayList<>();
        names.add("全部");
    }   //  初始化顶部导航names数据列表

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_bookkeeping, menu);
        MenuItem subtitleItem = menu.findItem(R.id.show_subtitle);
        subtitleItem.setTitle(mSubtitleVisible ? R.string.hide_subtitle : R.string.show_subtitle);
    }   //  创建并实例化右上角菜单栏

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.new_bill:
                Bill bill = new Bill();
                BillLab.get(getActivity()).addBill(bill);   //  向单例中添加bill
                Intent intent = BillPagerActivity
                        .newIntent(getActivity(), bill.getId());    //  新建用以启动BillPagerActivity的intent，传递ID
                startActivity(intent);  //  启动BillPagerActivity
                return true;
            case R.id.search:
                Intent intent1 = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent1);
                return true;
            case R.id.show_subtitle:
                mSubtitleVisible = !mSubtitleVisible;
                getActivity().invalidateOptionsMenu();
                updateSubtitle();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }   //  "添加""搜索"功能代码

    public void updateSubtitle() {
        BillLab billLab = BillLab.get(getActivity());
        int billCount = billLab.getBills().size();
        String subtitle = getString(R.string.subtitle_format, billCount);

        if (!mSubtitleVisible) {
            subtitle = null;
        }

        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.getSupportActionBar().setSubtitle(subtitle);
    }

    @Override
    public void onResume() {
        super.onResume();
        updateSubtitle();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putBoolean(SAVED_SUBTITLE_VISIBLE, mSubtitleVisible);
    }
}
